package com.jp.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AppleFilter {

    public  interface Fileter{
        boolean filter(Apple apple);
    }
    //方式1
    public static List<Apple> findGreenApple(List<Apple> apples) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if ("green".equals(apple.getColor())) {
                list.add(apple);
            }
        }
        return list;
    }
    //方式2
    public static List<Apple> findApple(List<Apple> apples,String color) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (color.equals(apple.getColor())) {
                list.add(apple);
            }
        }
        return list;
    }
    //方式3，策略模式
    public static List<Apple> findApple(List<Apple> apples,Fileter fileter) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (fileter.filter(apple)) {
                list.add(apple);
            }
        }
        return list;
    }
    public static  class GreenAnd150WeightFilter implements Fileter{
        @Override
        public boolean filter(Apple apple) {
            if(apple.getColor().equals("green") && apple.getWeight()==150){
                return  true;
            }
            return false;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        List<Apple> list = Arrays.asList(new Apple("green", 150),
                new Apple("yellow", 120), new Apple("green", 170));

        List<Apple> greenApples = findGreenApple(list);
        System.out.println(greenApples);

        greenApples=findApple(list,"yellow");
        System.out.println(greenApples);

        greenApples=findApple(list,new GreenAnd150WeightFilter());
        System.out.println(greenApples);

        //匿名内部类
        greenApples=findApple(list, new Fileter() {
            @Override
            public boolean filter(Apple apple) {
                if(apple.getColor().equals("green") && apple.getWeight()==150){
                    return  true;
                }
                return false;
            }
        });
        System.out.println(greenApples);

        //lambda
        greenApples=findApple(list, apple -> {
            if(apple.getColor().equals("green") && apple.getWeight()==150){
                return  true;
            }
            return false;
        });
        //lambda
        System.out.println(greenApples);
        new Thread(()->{System.out.println(Thread.currentThread().getName());}).start();
        Thread.currentThread().join();
    }
}
